/**
 * 
 */
package myTeam.timeShop.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import myTeam.timeShop.bean.TbChar;
import myTeam.timeShop.bean.TbUser;
import myTeam.timeShop.dao.CharDao;
import myTeam.timeShop.dao.UserDao;
import myTeam.timeShop.entity.Char;
import myTeam.timeShop.entity.User;
import myTeam.timeShop.service.CharService;

/**
 * @author jiangjintai
 *
 */
@Service("charService")
public class CharServiceImpl implements CharService {
	@Autowired
	CharDao charDao;
	@Autowired
	UserDao userDao;
	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#load(java.lang.Object)
	 */
	@Override
	public TbChar load(String id) {
		// TODO 自动生成的方法存根
		return charDao.load(id);
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#get(java.lang.Object)
	 */
	@Override
	public TbChar get(String id) {
		// TODO 自动生成的方法存根
		return charDao.get(id);
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#findAll()
	 */
	@Override
	public List<TbChar> findAll() {
		// TODO 自动生成的方法存根
		return charDao.findAll();
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#persist(java.lang.Object)
	 */
	@Override
	public void persist(TbChar entity) {
		// TODO 自动生成的方法存根
		charDao.persist(entity);
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#save(java.lang.Object)
	 */
	@Override
	public String save(TbChar entity) {
		// TODO 自动生成的方法存根
		return charDao.save(entity).toString();
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#saveOrUpdate(java.lang.Object)
	 */
	@Override
	public void saveOrUpdate(TbChar entity) {
		// TODO 自动生成的方法存根
		charDao.saveOrUpdate(entity);
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#delete(java.lang.Object)
	 */
	@Override
	public void delete(String id) {
		// TODO 自动生成的方法存根
		charDao.delete(id);
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.GenericService#flush()
	 */
	@Override
	public void flush() {
		// TODO 自动生成的方法存根
		charDao.flush();
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.CharService#addChar(java.lang.String, java.lang.String, java.lang.String)
	 */
	@Override
	
	public void addChar(String charTo, String filterUserId, String message) {
		TbUser charToUser = userDao.get(charTo);
		TbUser charFromUser = userDao.get(filterUserId);
		
		TbChar tbChar = new TbChar();
		tbChar.setCharIsSaw(0);
		tbChar.setCharMessage(message);
		tbChar.setCharTime(new Date());
		tbChar.setTbUserByCharFrom(charFromUser);
		tbChar.setTbUserByCharTo(charToUser);
		
		this.save(tbChar);
		
	}

	/* （非 Javadoc）
	 * @see myTeam.timeShop.service.CharService#getAllMyNoSeeChar(java.lang.String)
	 */
	@Override
	
	public List<TbChar> getAllMyNoSeeChar(String filterUserId) {
		List<TbChar> tbCharList= this.charDao.getAllMyNoSeeChar(filterUserId);
		List<TbChar> charList = new ArrayList<TbChar>();
		for(TbChar c : tbCharList){
			Char chars = new Char();
			chars.setCharFromUserId(c.getTbUserByCharFrom().getUserId());
			chars.setCharId(c.getCharId());
			chars.setCharIsSaw(c.getCharIsSaw());
			chars.setCharMessage(c.getCharMessage());
			chars.setCharTime(c.getCharTime());
			chars.setCharToUserId(c.getTbUserByCharTo().getUserId());
			chars.setTbUserByCharTo(null);
			
			User user = new User();
			user.setUserAccount(c.getTbUserByCharFrom().getUserAccount());
			user.setUserFacePath(c.getTbUserByCharFrom().getUserFacePath());
			user.setUserId(c.getTbUserByCharFrom().getUserId());
			user.setUserLevel(c.getTbUserByCharFrom().getUserLevel());
			user.setUserName(c.getTbUserByCharFrom().getUserName());
			user.setUserSchool(c.getTbUserByCharFrom().getUserSchool());
			user.setUserSex(c.getTbUserByCharFrom().getUserSex());
			chars.setTbUserByCharFrom(user);
			charList.add(chars);
		}
		return  charList;
	}

}
